package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackInfoMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackMediaInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Random;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class TrackInfoServiceImpl extends ServiceImpl<TrackInfoMapper, TrackInfo> implements TrackInfoService {

    @Autowired
    private TrackInfoMapper trackInfoMapper;
    @Autowired
    private AlbumInfoMapper albumInfoMapper;
    @Autowired
    private VodService vodService;
    @Autowired
    private TrackStatMapper trackStatMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTrackInfo(TrackInfoVo trackInfoVo, Long userId) {
   //先整个shengyin空对象
        TrackInfo trackInfo = new TrackInfo();
     //赋值拷贝到trackInfo对象中
        BeanUtils.copyProperties(trackInfoVo,trackInfo);
        //还有个userId参数，这里直接赋值
        trackInfo.setUserId(userId);
        //order_num  include_track_count+1
        //先获取到专辑对象
        AlbumInfo albumInfo = this.albumInfoMapper.selectById(trackInfo.getAlbumId());
        //它将当前曲目的顺序号（order_num）设置为专辑中已包含的曲目数量 (include_track_count) 加 1。
        trackInfo.setOrderNum(albumInfo.getIncludeTrackCount()+1);
        //给流媒体数据赋值,通过声音Id查找声音数据:  vod-api
        TrackMediaInfoVo trackMediaInfoVo = vodService.getMediaInfo(trackInfoVo.getMediaFileId());
        if(null!=trackMediaInfoVo){
            trackInfo.setMediaDuration(trackMediaInfoVo.getDuration());
            trackInfo.setMediaUrl(trackMediaInfoVo.getMediaUrl());
            trackInfo.setMediaType(trackMediaInfoVo.getType());
            trackInfo.setMediaSize(trackMediaInfoVo.getSize());
        }
        //保存到数据库
        trackInfoMapper.insert(trackInfo);
        //album_info   include_track_count
        //更新专辑的包含曲目数量
        albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount()+1);
        this.albumInfoMapper.updateById(albumInfo);

        //track_stat 播放量啥的存起来
        this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_PLAY);
        this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_COLLECT);
        this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_COMMENT);
        this.saveTrackStat(trackInfo.getId(), SystemConstant.TRACK_STAT_PRAISE);


    }

    @Override
    public IPage<TrackListVo> findUserTrackPage(Page<TrackListVo> page, TrackInfoQuery trackInfoQuery) {

        return trackInfoMapper.findUserTrackPage(page, trackInfoQuery) ;
    }

    @Override
    public void removeTrackInfo(Long trackId) {
        //先获取声音对象
        TrackInfo trackInfo = this.trackInfoMapper.selectById(trackId);
        //删这张视图表
        this.removeById(trackId);
        //  track_stat 也删了
        trackStatMapper.delete(new LambdaQueryWrapper<TrackStat>().eq(TrackStat::getTrackId, trackId));
        //  album_info; include_track_count-1;
        AlbumInfo albumInfo = this.albumInfoMapper.selectById(trackInfo.getAlbumId());
        albumInfo.setIncludeTrackCount(albumInfo.getIncludeTrackCount()-1);
        this.albumInfoMapper.updateById(albumInfo);

        trackInfoMapper.updateOrderNum(trackInfo.getAlbumId(), trackInfo.getOrderNum());

        try {
            vodService.removeMedia(trackInfo.getMediaFileId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public void updateTrackInfo(Long trackId, TrackInfoVo trackInfoVo) {
        //获取trackInfo对象
        TrackInfo trackInfo = this.getById(trackId);
        //获取原有声音id
        String oldMediaFileId = trackInfo.getMediaFileId();

        //赋值拷贝
        BeanUtils.copyProperties(trackInfoVo,trackInfo);
        //根据老id删除云点播
        if (!oldMediaFileId.equals(trackInfoVo.getMediaFileId())) {
            //不相同是修改了，删除老的
            vodService.removeMediaByFileId(oldMediaFileId);
            //获取新的声音流媒体信息
            TrackMediaInfoVo trackMediaInfoVo = vodService.getMediaInfo(trackInfoVo.getMediaFileId());
            //新的流媒体扔进去
            trackInfo.setMediaDuration(trackMediaInfoVo.getDuration());
            trackInfo.setMediaUrl(trackMediaInfoVo.getMediaUrl());
            trackInfo.setMediaType(trackMediaInfoVo.getType());
            trackInfo.setMediaSize(trackMediaInfoVo.getSize());

        }
        trackInfoMapper.updateById(trackInfo);
    }

    private void saveTrackStat(Long trackId,String tracType) {
        //保存数据
        TrackStat ts = new TrackStat();
        ts.setTrackId(trackId);
        ts.setStatType(tracType);
        ts.setStatNum(new Random().nextInt(1000));
        this.trackStatMapper.insert(ts);

    }
}
